package com.code.day21;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class DemoServerClient {
    public static void main(String[] args) throws Exception {
        //1.创建ServerSocket对象,设置端口号
        ServerSocket serverSocket = new ServerSocket(9999);
        //2.调用ServerSocket中的accept方法,用于接收连接过来的客户端对象,此方法返回Socket对象
        Socket socket = serverSocket.accept();
        //3.调用socket中的getInputStream,用于读取客户端发送过来的请求
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        System.out.println(new String(bytes, 0, read));
        //4.调用socket中的getOutputStream,用于给客户端写响应
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("我是超级无敌刘大胆".getBytes());
        //关流
        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}
